package com.zlyx.easy.core.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.zlyx.easy.core.map.EasyMap;
import com.zlyx.easy.core.tool.Ops;

/**
 * @author 赵光
 * @desc Map封装
 * @Date 2018年11月26日 下午4:24:19
 */
public class EasyMap<K, V> extends HashMap<K, V> {

	private static final long serialVersionUID = 1L;

	public EasyMap() {
	}

	public EasyMap<K, V> addValue(K key, V value) {
		this.put(key, value);
		return this;
	}

	public EasyMap<K, V> addValue(Map<K, V> paramMap) {
		putAll(paramMap);
		return this;
	}

	/**
	 * 替换key
	 * 
	 * @param oldKey
	 * @param newKey
	 * @return
	 */
	public EasyMap<K, V> replaceKey(K oldKey, K newKey) {
		if (containsKey(oldKey)) {
			put(newKey, remove(oldKey));
		}
		return this;
	}

	/**
	 * 移除值为Null或""的key
	 * 
	 * @return
	 */
	public EasyMap<K, V> removeKeyWithEmptyValue() {
		for (Entry<K, V> entry : entrySet()) {
			if (Ops.isEmpty(entry.getValue())) {
				remove(entry.getKey());
			}
		}
		return this;
	}

	/**
	 * 获得key集合
	 * 
	 * @return
	 */
	public List<K> getKeys() {
		return new ArrayList<>(keySet());
	}

	/**
	 * 获得value集合
	 * 
	 * @return
	 */
	public List<V> getValues() {
		return new ArrayList<V>(values());
	}

	public boolean withoutKey(String key) {
		return !containsKey(key);
	}

	public boolean notEmpty() {
		return !isEmpty();
	}

	@Override
	public String toString() {
		return JSON.toJSONString(this);
	}

	public static <K, V> EasyMap<K, V> newMap() {
		return new EasyMap<K, V>();
	}

	public static <K, V> EasyMap<K, V> newMap(K key, V value) {
		return new EasyMap<K, V>().addValue(key, value);
	}

	public static <K, V> EasyMap<K, V> newMap(Map<K, V> map) {
		return new EasyMap<K, V>().addValue(map);
	}
}
